﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Entities;
using System.ComponentModel;
using Cesave.Application.Permisos.ServiceAgent;

namespace Cesave.Presentation.Permisos.WinForms.Client.ViewModels.Permisos
{
	public partial class ExpedicionViewModel
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private FoliosPermiso _foliosPermiso;

		#endregion

		#region Public properties

		/// <summary>
		/// 
		/// </summary>
		public FoliosPermiso FoliosPermiso
		{
			get
			{
				return _foliosPermiso;
			}
			private set
			{
				_foliosPermiso = value;
				if (_foliosPermiso != null)
					RaisePropertyChanged("FoliosPermiso");
			}
		}

		#endregion

		#region Private methods

		/// <summary>
		/// 
		/// </summary>
		public void LoadFoliosPermiso()
		{
			if (IsCicloReady && IsAreaReady)
			{
				StartWaiting();
				using (BackgroundWorker worker = new BackgroundWorker())
				{
					worker.DoWork += (sender, e) =>
					{
						using (PermisosModuleServiceWrapper service = new PermisosModuleServiceWrapper())
						{
							e.Result = service.FindFolioPermisoByJuntaAndCicloAndArea(_pusSettings.Junta.JuntaGuid, Ciclo.CicloGuid, Area.AreaGuid);
						}
					};
					worker.RunWorkerCompleted += (sender, e) =>
					{
						EndWaiting();
						if (!e.Cancelled && e.Error == null)
							FoliosPermiso = e.Result as FoliosPermiso;
						else if (e.Error != null)
							ShowMessage(e.Error.Message);
					};
					worker.WorkerSupportsCancellation = true;
					worker.RunWorkerAsync();
				}
			}
		}

		#endregion

		#region Event methods

		private void OnFoliosPermisoPropertyChanged()
		{
			_expedicionView.foliosPermisoBindingSource.DataSource = FoliosPermiso;
		}

		#endregion

	}
}
